﻿using Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfCommCtl.Controls
{
    internal class LogInfo : NotifyBase
    {
        public LogInfo(string log)
        {
            //string timeStamp = DateTime.Now.ToString("MM-dd HH:mm:ss");
            string timeStamp = DateTime.Now.ToString("HH:mm:ss");
            Time = timeStamp;
            if (log != null)
            {
                IsOK = !log.Contains("!");
            }
            else
            {
                IsOK = true;
            }
            Content = log;
        }

        private string _time;

        public string Time
        {
            get { return _time; }
            set
            {
                _time = value;
                this.DoNotify();
            }
        }

        private string _content;

        public string Content
        {
            get { return _content; }
            set
            {
                _content = value;
                this.DoNotify();
            }
        }

        private bool _isOK;

        public bool IsOK
        {
            get { return _isOK; }
            set
            {
                _isOK = value;
                this.DoNotify();
            }
        }
    }

    /// <summary>
    /// LogView.xaml 的交互逻辑
    /// </summary>
    public partial class LogView : UserControl
    {
        private ObservableCollection<LogInfo> Logs = new ObservableCollection<LogInfo>();

        /// <summary>
        /// ctor
        /// </summary>
        public LogView()
        {
            InitializeComponent();
            //this.listViewLog.ItemsSource = this.Logs;
            gdLogs.ItemsSource = this.Logs;
        }

        /// <summary>
        /// update log
        /// </summary>
        /// <param name="str"> </param>
        public void LogUpdate(string str)
        {
            if (Logs.Count > 10000)
            {
                Logs.Clear();
            }
            Logs.Add(new LogInfo(str));
            //listViewLog.ScrollIntoView(Logs[Logs.Count - 1]);
            gdLogs.ScrollIntoView(Logs[Logs.Count - 1]);
        }

        /// <summary>
        /// log
        /// </summary>
        public string Log
        {
            get { return (string)GetValue(LogProperty); }
            set { SetValue(LogProperty, value); }
        }

        /// <summary>
        /// log
        /// </summary>
        public static readonly DependencyProperty LogProperty =
            DependencyProperty.Register("Log", typeof(string), typeof(LogView), new PropertyMetadata(string.Empty, new PropertyChangedCallback(LogPropertyChangedCallback)));

        private static void LogPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as LogView).LogChanged();
        }

        private void LogChanged()
        {
            LogUpdate(Log);
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Logs.Clear();
        }
    }
}